<template>
	<om-page>
		<view class="padding-xl text-center text-xxl bg-steelblue">安全检测</view>
		<view class="content">
			<!-- #ifdef H5 || APP-PLUS -->
			<view v-if="mobile">
				<view v-if="showTip" class="padding-xl text-center text-sm">我们已发送验证码到您的手机：
					<view class="padding-top text-center text-xl text-bold">{{mobile}}</view>
					<view v-if="retainnum != null" class="padding-top text-center text-sm">今日还可以获取 <text class="text-xl text-bold text-italic padding-left-sm padding-right-sm">{{retainnum}}</text> 次验证码</view>
				</view>
				<view class="input-group">
					<view class="cu-form-group">
						<view class="title">验证码</view>
						<input placeholder="请输入验证码" v-model="verifyCode"></input>
						<view v-if="seconds" class='cu-tag round bg-grey'>{{seconds}}s</view>
						<button v-else class='cu-btn shadow' :class="[bgColor]" @click="getVerifyCode">{{verifybtnText}}</button>
					</view>
				</view>
				<view class="btn-row"><button type="warn" class="primary" @tap="bindNext">下一步</button></view>
				<om-wing-blank class="tui-flex justify-end">
					<view @click="login" class='cu-tag radius bg-grey'>登录</view>
					<view class='cu-tag radius bg-grey' @click="errorMobile">手机号码不对</view>
				</om-wing-blank>
			</view>
			<view v-else>
				<view class="padding-top text-center text-xl text-bold">{{errorTip}}</view>
			</view>
			<!-- #endif -->
		</view>
	</om-page>
</template>

<script>
	import {ignoreHtml, errorModal } from 'static/js/common.js'
	const app = getApp();
	export default {
		data() {
			return {
				idcard:"",
				verifyCode:"",
				showTip:false,
				seconds: 1,
				verifybtnText: "获取验证码",
				mobile: "",
				errorTip: "手机号异常，请联系管理员！",
				bgColor:"bg-green",
				retainnum:null,
				interval:null
			};
		},
		computed: {

		},
		methods: {
			login(){
				uni.reLaunch({
					url:"../login/login"
				})
			},
			dumiao() {
				let _that = this
				_that.interval = setInterval(function() {
					if (_that.seconds && _that.seconds > 0) {
						_that.seconds -= 1
					} else if (_that.interval) {
						clearInterval(_that.interval);
					}
				}, 1000)
			},
			errorMobile(){
				this.om.toast("请找管理员修改手机号",5000)
			},
			bindNext(){
				let _that = this
				if(this.verifyCode && this.idcard){
					this.om.request("temployee/verifyCode",{
						idcard:this.idcard,
						verifyCode:this.verifyCode
					}).then(res=>{
						if(res.succeed){
							if(res.value){
								uni.navigateTo({
									url:"confirmpwd?idcard=" + _that.idcard + "&verifyCode=" + _that.verifyCode
								})
							}else{
								_that.om.toast("验证码错误")
							}
						}else{
							_that.om.toast(ignoreHtml(res.msg))
						}
					})
				}
			},
			getVerifyCode(){
				this.showTip = false
				if(!this.seconds){
					let _that = this
					this.om.request("smsPost/getVerifyCode2",{idcard:this.idcard}).then(res=>{
						if(res.succeed){
							let map = res.value
							_that.retainnum = parseInt(map.num)
							_that.mobile = map.mobile
							_that.showTip = true
						}else{
							_that.om.toast(ignoreHtml(res.msg))
						}
					})
				}
			}
		},
		onLoad(e) {
			if(!e.idcard){
				errorModal(this,"验证异常","请重新验证身份",function(){
					uni.redirectTo({
						url:"forgetpwd"
					})
				})
			}else{
				this.idcard = e.idcard
			}
			if(e.retainnum){
				this.retainnum = parseInt(e.retainnum)
			}
			if (e.mobile) {
				this.mobile = e.mobile
				this.seconds = 60
			}
			this.showTip = true
			this.dumiao()

		}
	};
</script>

<style>
	.forgetPwd {
		text-align: right;
		font-size: 14px;
		padding-top: 15px;
		text-decoration: none;
		color: #0faeff;
	}
</style>
